package cn.dragon.eatingfish;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import static cn.dragon.eatingfish.TipMusicPlayer.playTipMusic;

public class World extends JPanel {
    public static final int WIDTH = 1400;
    public static final int HEIGHT = 700;
    public static final int RUNNING = 0;   //运行状态
    public static final int GAME_OVER = 1; //游戏结束状态
    public static final int SIMPLE_LEVEL = 0;
    public static final int MEDIUM_LEVEL = 1;
    public static final int HARD_LEVEL = 2;
    // 音乐常量
    private static final String EAT_FISH = "music/eating.mp3";
    private static final String EAT_TOOL = "music/eatTool.mp3";
    private static final String LEVEL_UP = "music/levelUp.mp3";
    private static final String MAJOR_DEAD = "music/majorFishDead.mp3";
    public static int checkpoint;//当前默认游戏关卡为简单关卡
    public static int allnpcnum;
    public static int allNpc_SevenNum = 0;
    private static int lX;       // 10ms前的x坐标，用于判断鱼头向左还是向右
    private static int lY;       // 10ms前的y坐标，用于判断鱼头向左还是向右
    private static World world = new World();
    private static GameMusic bgm1 = new GameMusic();     // 开始背景音乐
    private static GameMusic bgm2 = new GameMusic();     // 运行背景音乐
    private static MajorFish majorFish = new MajorFish();   // 主角鱼
    private static boolean winFlag = false;
    private static boolean failFlag = false;
    private SeaObject[] fishes = {};     // 其他鱼类数组
    private Drop[] drops = {};       // 道具数组
    private int score = 0;       // 分数
    private int sleepDead = 0;   // 主角鱼的生命减少延迟
    private boolean flashFlag = false;   // 主角鱼闪烁标记
    private int flashTime = 0;   // 主角鱼闪烁时间
    // NPC进入
    private int dropTime = 0;  // 掉落时间标记
    private int fishEnterIndex = 0;
    private int state = RUNNING; //当前状态(默认为启动状态)
    private int num = 0;
    private Point[] points = {}; // 得分标志数组
    private boolean deadFlag = false;


    public World() {

    }

    // 首页选项
    public static void menu() {
        JFrame jf = new JFrame("大鱼吃小鱼");

        jf.setSize(1400 + 19, 700 + 48);
//        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setLayout(null);
        jf.setResizable(false); // 设置窗口大小无法改变
        jf.setIconImage(Images.logo.getImage());
        jf.setLocation(250, 150);

        JButton lowLevelBtn = new JButton();
        lowLevelBtn.setSize(200, 78);
        lowLevelBtn.setBounds(300, 450, 200, 95);
        // 设置按钮的默认图片
        lowLevelBtn.setIcon(new ImageIcon("images/lowLevel.png"));
        // 设置按钮被点击时的图片
        lowLevelBtn.setPressedIcon(new ImageIcon("images/lowLevel_press.png"));
        // 不绘制边框
        lowLevelBtn.setBorderPainted(false);
        lowLevelBtn.setContentAreaFilled(false);    //不绘制默认按钮背景
        lowLevelBtn.setFocusPainted(false);           //不绘制图片或文字周围的焦点虚框

        jf.add(lowLevelBtn);


        // 添加按钮点击事件监听器
        lowLevelBtn.addActionListener(e -> {
            checkpoint = SIMPLE_LEVEL;
            allnpcnum = 10;
            game();
            world.state = RUNNING;
            jf.dispose();
            System.gc();
        });
        JButton midLevelBtn = new JButton();
        midLevelBtn.setSize(200, 78);
        // 设置按钮的默认图片
        midLevelBtn.setBounds(600, 450, 200, 95);
        midLevelBtn.setIcon(new ImageIcon("images/midLevel.png"));
        // 设置按钮被点击时的图片
        midLevelBtn.setPressedIcon(new ImageIcon("images/midLevel_press.png"));
        // 不绘制边框
        midLevelBtn.setBorderPainted(false);
        midLevelBtn.setContentAreaFilled(false);    //不绘制默认按钮背景
        midLevelBtn.setFocusPainted(false);           //不绘制图片或文字周围的焦点虚框
        jf.add(midLevelBtn);
        midLevelBtn.addActionListener(e -> {
            checkpoint = MEDIUM_LEVEL;
            allnpcnum = 15;
            game();
            world.state = RUNNING;
            jf.dispose();
            System.gc();

        });

        JButton highLevelBtn = new JButton();
        highLevelBtn.setSize(200, 78);
        // 设置按钮的默认图片
        highLevelBtn.setBounds(900, 450, 200, 95);
        highLevelBtn.setIcon(new ImageIcon("images/highLevel.png"));
        // 设置按钮被点击时的图片
        highLevelBtn.setPressedIcon(new ImageIcon("images/highLevel_press.png"));
        // 不绘制边框
        highLevelBtn.setBorderPainted(false);
        highLevelBtn.setContentAreaFilled(false);    //不绘制默认按钮背景
        highLevelBtn.setFocusPainted(false);           //不绘制图片或文字周围的焦点虚框
        jf.add(highLevelBtn);
        highLevelBtn.addActionListener(e -> {
            checkpoint = HARD_LEVEL;
            allnpcnum = 20;
            game();
            world.state = RUNNING;
            jf.dispose();
            System.gc();
        });
        // 添加背景
        ImageIcon imageIcon = new ImageIcon("images/menu_bg.png");
        JLabel label = new JLabel(imageIcon);
        label.setBounds(0, 0, 1419, 748);
        jf.add(label);
        jf.setVisible(true);
        bgm1.palyLoopMusic("music/backmusic.wav");

    }

    public static void game() {
        System.out.println("按钮被点击了");
        playTipMusic("music/bgm.mp3");
        JFrame frame = new JFrame("大鱼吃小鱼");
        frame.setIconImage(Images.logo.getImage());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(WIDTH + 19, HEIGHT + 48);
//        frame.setLocationRelativeTo(null);
        frame.setLocation(100, 150);
        frame.setVisible(true);
        // 保证窗口改变图片能显示
        frame.setResizable(false);
        world.setFocusable(true);
        frame.add(world);
        MouseAdp a = new MouseAdp();
        frame.addMouseListener(a);          // 添加鼠标监听事件（点击）
        frame.addMouseMotionListener(a);    // 添加鼠标位置监听
        world.action();
        TipWindow tip = new TipWindow();
        tip.paintIntroduce();
        tip.action();
    }

    public static int getLX() {
        return lX;
    }

    public static int getLY() {
        return lY;
    }

    public static MajorFish getMajorFish() {
        return majorFish;
    }

    // 按主角鱼等级生成NPC对象
    public SeaObject newFish() {
        Random rand = new Random();
        int type = rand.nextInt(100);
        if (checkpoint == SIMPLE_LEVEL) {
            // 控制概率
            switch (majorFish.getLevel()) {
                case 1:
                    if (type < 80) {
                        return new NPC_1();
                    } else if (type < 90) {
                        return new NPC_2();
                    } else {
                        return new NPC_3();
                    }
                case 2:
                    if (type < 65) { // 控制概率
                        return new NPC_1();
                    } else if (type < 75) {
                        return new NPC_2();
                    } else if (type < 95) {
                        return new NPC_3();
                    } else {
                        return new NPC_4();
                    }
                case 3:
                    if (type < 25) { // 控制概率
                        return new NPC_1();
                    } else if (type < 50) {
                        return new NPC_2();
                    } else if (type < 70) {
                        return new NPC_3();
                    } else if (type < 95) {
                        return new NPC_4();
                    } else {
                        return new NPC_5();
                    }
                case 4:
                    if (type < 15) { // 控制概率
                        return new NPC_1();
                    } else if (type < 30) {
                        return new NPC_2();
                    } else if (type < 50) {
                        return new NPC_3();
                    } else if (type < 70) {
                        return new NPC_4();
                    } else if (type < 85) {
                        return new NPC_5();
                    } else {
                        return new NPC_6();
                    }
                case 5:
                    if (type < 15) { // 控制概率
                        return new NPC_1();
                    } else if (type < 25) {
                        return new NPC_2();
                    } else if (type < 40) {
                        return new NPC_3();
                    } else if (type < 55) {
                        return new NPC_4();
                    } else if (type < 70) {
                        return new NPC_5();
                    } else if (type < 90) {
                        return new NPC_6();
                    } else {
                        if (allNpc_SevenNum < 3) {
                            allNpc_SevenNum++;
                            return new NPC_7();
                        } else {
                            return new NPC_6();
                        }
                    }
                case 6:
                    if (type < 10) { // 控制概率
                        return new NPC_1();
                    } else if (type < 15) {
                        return new NPC_2();
                    } else if (type < 25) {
                        return new NPC_3();
                    } else if (type < 35) {
                        return new NPC_4();
                    } else if (type < 55) {
                        return new NPC_5();
                    } else if (type < 80) {
                        return new NPC_6();
                    } else {
                        if (allNpc_SevenNum < 3) {
                            allNpc_SevenNum++;
                            return new NPC_7();
                        } else {
                            return new NPC_6();
                        }
                    }
            }
        } else if (checkpoint == MEDIUM_LEVEL) {
            switch (majorFish.getLevel()) {
                case 1:
                    if (type < 75) {
                        return new NPC_1();
                    } else if (type < 90) {
                        return new NPC_2();
                    } else {
                        return new NPC_3();
                    }
                case 2:
                    if (type < 65) { // 控制概率
                        return new NPC_1();
                    } else if (type < 75) {
                        return new NPC_2();
                    } else if (type < 90) {
                        return new NPC_3();
                    } else {
                        return new NPC_4();
                    }
                case 3:
                    if (type < 25) { // 控制概率
                        return new NPC_1();
                    } else if (type < 55) {
                        return new NPC_2();
                    } else if (type < 70) {
                        return new NPC_3();
                    } else if (type < 90) {
                        return new NPC_4();
                    } else {
                        return new NPC_5();
                    }
                case 4:
                    if (type < 10) { // 控制概率
                        return new NPC_1();
                    } else if (type < 30) {
                        return new NPC_2();
                    } else if (type < 50) {
                        return new NPC_3();
                    } else if (type < 65) {
                        return new NPC_4();
                    } else if (type < 85) {
                        return new NPC_5();
                    } else {
                        return new NPC_6();
                    }
                case 5:
                    if (type < 5) { // 控制概率
                        return new NPC_1();
                    } else if (type < 15) {
                        return new NPC_2();
                    } else if (type < 30) {
                        return new NPC_3();
                    } else if (type < 50) {
                        return new NPC_4();
                    } else if (type < 60) {
                        return new NPC_5();
                    } else if (type < 90) {
                        return new NPC_6();
                    } else {
                        if (allNpc_SevenNum < 4) {
                            allNpc_SevenNum++;
                            return new NPC_7();
                        } else {
                            return new NPC_5();
                        }
                    }
                case 6:
                    if (type < 15) { // 控制概率
                        return new NPC_1();
                    } else if (type < 25) {
                        return new NPC_2();
                    } else if (type < 35) {
                        return new NPC_3();
                    } else if (type < 45) {
                        return new NPC_4();
                    } else if (type < 60) {
                        return new NPC_5();
                    } else if (type < 75) {
                        return new NPC_6();
                    } else {
                        if (allNpc_SevenNum < 4) {
                            allNpc_SevenNum++;
                            return new NPC_7();
                        } else {
                            return new NPC_6();
                        }
                    }
            }
        } else {
            switch (majorFish.getLevel()) {
                case 1:
                    if (type < 72) {
                        return new NPC_1();
                    } else if (type < 90) {
                        return new NPC_2();
                    } else {
                        return new NPC_3();
                    }
                case 2:
                    if (type < 60) { // 控制概率
                        return new NPC_1();
                    } else if (type < 70) {
                        return new NPC_2();
                    } else if (type < 90) {
                        return new NPC_3();
                    } else {
                        return new NPC_4();
                    }
                case 3:
                    if (type < 20) { // 控制概率
                        return new NPC_1();
                    } else if (type < 46) {
                        return new NPC_2();
                    } else if (type < 66) {
                        return new NPC_3();
                    } else if (type < 90) {
                        return new NPC_4();
                    } else {
                        return new NPC_5();
                    }
                case 4:
                    if (type < 10) { // 控制概率
                        return new NPC_1();
                    } else if (type < 30) {
                        return new NPC_2();
                    } else if (type < 50) {
                        return new NPC_3();
                    } else if (type < 62) {
                        return new NPC_4();
                    } else if (type < 85) {
                        return new NPC_5();
                    } else {
                        return new NPC_6();
                    }
                case 5:
                    if (type < 9) { // 控制概率
                        return new NPC_1();
                    } else if (type < 17) {
                        return new NPC_2();
                    } else if (type < 27) {
                        return new NPC_3();
                    } else if (type < 42) {
                        return new NPC_4();
                    } else if (type < 49) {
                        return new NPC_5();
                    } else if (type < 84) {
                        return new NPC_6();
                    } else {
                        if (allNpc_SevenNum < 5) {
                            allNpc_SevenNum++;
                            return new NPC_7();
                        } else {
                            return new NPC_6();
                        }
                    }
                case 6:
                    if (type < 12) { // 控制概率
                        return new NPC_1();
                    } else if (type < 22) {
                        return new NPC_2();
                    } else if (type < 30) {
                        return new NPC_3();
                    } else if (type < 37) {
                        return new NPC_4();
                    } else if (type < 52) {
                        return new NPC_5();
                    } else if (type < 60) {
                        return new NPC_6();
                    } else {
                        if (allNpc_SevenNum < 5) {
                            allNpc_SevenNum++;
                            return new NPC_7();
                        } else {
                            return new NPC_6();
                        }
                    }
            }
        }
        return null;
    }

    public Drop nextDrop() {
        Random rand = new Random();
        int type = rand.nextInt(4);
        if (type == 0) {
            return new Eliminate();
        } else if (type == 1) {
            return new Freeze();
        } else if (type == 2) {
            return new Life();
        } else return new Score();
    }

    // 设置鱼静止后能够继续移动
    public void continueMove() {
        num++;
        if (num % 400 == 0) {
            for (SeaObject fish : fishes) {
                fish.setFreezing(false);
                if (fish instanceof NPC_1) fish.setSpeed(5);
                else if (fish instanceof NPC_2) fish.setSpeed(3);
                else if (fish instanceof NPC_3) fish.setSpeed(2);
                else if (fish instanceof NPC_4) fish.setSpeed(2);
                else if (fish instanceof NPC_5) fish.setSpeed(2);
                else if (fish instanceof NPC_6) fish.setSpeed(1);
                else if (fish instanceof NPC_7) fish.setSpeed(1);
            }
        }
    }

    // 更改当前等级
    public void changeLevel() {
        int currentLevel = majorFish.getLevel();
        if (checkpoint == SIMPLE_LEVEL) {
            if (score <= 100) {
                majorFish.setLevel(1);
            } else if (score <= 250) {
                majorFish.setLevel(2);
            } else if (score <= 450) {
                majorFish.setLevel(3);
            } else if (score <= 700) {
                majorFish.setLevel(4);
            } else if (score <= 1000) {
                majorFish.setLevel(5);
            } else {
                majorFish.setLevel(6);
            }
            if (currentLevel != majorFish.getLevel()) {
                playTipMusic(LEVEL_UP);
            }
        } else if (checkpoint == MEDIUM_LEVEL) {
            if (score <= 300) {
                majorFish.setLevel(1);
            } else if (score <= 500) {
                majorFish.setLevel(2);
            } else if (score <= 900) {
                majorFish.setLevel(3);
            } else if (score <= 1500) {
                majorFish.setLevel(4);
            } else if (score <= 2300) {
                majorFish.setLevel(5);
            } else {
                majorFish.setLevel(6);
            }
            if (currentLevel != majorFish.getLevel()) {
                playTipMusic(LEVEL_UP);
            }
        } else if (checkpoint == HARD_LEVEL) {
            if (score <= 400) {
                majorFish.setLevel(1);
            } else if (score <= 800) {
                majorFish.setLevel(2);
            } else if (score <= 1400) {
                majorFish.setLevel(3);
            } else if (score <= 2200) {
                majorFish.setLevel(4);
            } else if (score <= 3200) {
                majorFish.setLevel(5);
            } else {
                majorFish.setLevel(6);
            }
            if (currentLevel != majorFish.getLevel()) {
                playTipMusic(LEVEL_UP);
            }
        }
    }

    // 将生成的npc存入数组
    public void objectEnterAction() { // 默认间隔时间是10ms
        fishEnterIndex++; // 每10ms自增1
        if (fishEnterIndex % 20 == 0) { // 控制间隔时间频率，400ms生成一次对象
            if (fishes.length <= allnpcnum) { // 控制总体鱼数不让无限制增长
                SeaObject obj = newFish(); // 调用newFish()将随机对象向上造型
                fishes = Arrays.copyOf(fishes, fishes.length + 1); // 每有一个新对象就扩容数组
                fishes[fishes.length - 1] = obj; // 将该新对象添加到submarines数组末尾
            }
        }
        if (checkpoint == SIMPLE_LEVEL) {
            dropTime++;
            if (dropTime % 400 == 0) {
                Drop d = nextDrop();
                drops = Arrays.copyOf(drops, drops.length + 1);
                drops[drops.length - 1] = d;
            }
        } else if (checkpoint == MEDIUM_LEVEL) {
            dropTime++;
            if (dropTime % 500 == 0) {
                Drop d = nextDrop();
                drops = Arrays.copyOf(drops, drops.length + 1);
                drops[drops.length - 1] = d;
            }
        } else {
            dropTime++;
            if (dropTime % 600 == 0) {
                Drop d = nextDrop();
                drops = Arrays.copyOf(drops, drops.length + 1);
                drops[drops.length - 1] = d;
            }
        }
    }

    // 处理npc越界以及npc死亡后的处理 避免数组长度过长
    public void outOfBoundAction() {
        for (int i = 0; i < fishes.length; i++) {
            if (fishes[i].isOutOfBounds() || fishes[i].isDead()) {
                fishes[i] = fishes[fishes.length - 1]; //修改越界元素为最后一个元素
                fishes = Arrays.copyOf(fishes, fishes.length - 1); //缩容
            }
        }
        for (int i = 0; i < drops.length; i++) {
            if (drops[i].isOutOfaction() || drops[i].isDead()) {
                drops[i] = drops[drops.length - 1];
                drops = Arrays.copyOf(drops, drops.length - 1);
            }
        }
    }

    // 得分提示消失事件
    public void pointHiddenAction() {
        for (int i = 0; i < points.length; i++) {
            points[i].setCount(points[i].getCount() + 1);
            if (points[i].getCount() >= 30) {
                Point[] copyPoints = new Point[points.length - 1];
                System.arraycopy(points, 1, copyPoints, 0, copyPoints.length);
                points = copyPoints;
            }
        }
    }

    // 鱼的移动事件
    public void fishMoveAction() {
        majorFish.move(MouseAdp.x, MouseAdp.y);
        for (SeaObject fish : fishes) {
            fish.move(majorFish);
        }
        for (Drop drop : drops) {
            drop.move();
        }
    }

    // 判断游戏是否结束
    public void checkGameOverAction() {
        if (majorFish.getLife() < 1) {
            state = GAME_OVER;
            if (deadFlag == false) {
                playTipMusic(MAJOR_DEAD);
                deadFlag = true;
            }
        }
    }

    public void eatFishesAction() {
        for (SeaObject npc : fishes) {// 等价于for(int i = 0; i < fishes.length; i++_
            if (npc.isHit(majorFish)) { // 如果他们相撞了，则比较主角鱼与npc等级差别
                if (npc.compareLevel(majorFish)) {
                    // 主角鱼等级 >= npc等级
                    if (majorFish.isLive() && npc.isLive()) { // 都活着
                        npc.goDead();

                        Point p = new Point();
                        p.setX(majorFish.getX());
                        if (majorFish.getY() > 150) {
                            p.setY(majorFish.getY() - 100);
                        } else {
                            p.setY(majorFish.getY() + 50);
                        }
                        p.setPoint(npc.getScore());
                        points = Arrays.copyOf(points, points.length + 1); // 每有一个新对象就扩容数组
                        points[points.length - 1] = p;

                        playTipMusic(EAT_FISH);

                        if (npc instanceof FishScore) {
                            FishScore fs = (FishScore) npc;
                            score += fs.getScore();
                        }
                    }
                } else { // 主角鱼等级 < npc等级
                    if (majorFish.isLive() && npc.isLive()) {
                        if (sleepDead % 40 == 0) {
                            majorFish.goDead();
                            flashFlag = true;
                        }
                        sleepDead++;
                    }
                }
            }
        }
        for (Drop npc : drops) {
            if (majorFish.isLive() && npc.isLive() && npc.isHit(majorFish)) {
                npc.goDead();
                if (npc instanceof FishScore) {
                    FishScore es = (FishScore) npc;
                    score += es.getScore();
                    Point p = new Point();
                    p.setX(majorFish.getX());
                    if (majorFish.getY() > 150) {
                        p.setY(majorFish.getY() - 100);
                    } else {
                        p.setY(majorFish.getY() + 50);
                    }
                    p.setPoint(-4);
                    points = Arrays.copyOf(points, points.length + 1); // 每有一个新对象就扩容数组
                    points[points.length - 1] = p;
                }
                if (npc instanceof Life) {
                    Life es = (Life) npc;
                    majorFish.addLife(es.getLife());
                    Point p = new Point();
                    p.setX(majorFish.getX());
                    if (majorFish.getY() > 150) {
                        p.setY(majorFish.getY() - 100);
                    } else {
                        p.setY(majorFish.getY() + 50);
                    }
                    p.setPoint(-3);
                    points = Arrays.copyOf(points, points.length + 1); // 每有一个新对象就扩容数组
                    points[points.length - 1] = p;
                }
                if (npc instanceof Freeze) {
                    for (SeaObject b : fishes) {
                        b.setSpeed(0);
                        b.setFreezing(true);
                    }
                    Point p = new Point();
                    p.setX(majorFish.getX());
                    if (majorFish.getY() > 150) {
                        p.setY(majorFish.getY() - 100);
                    } else {
                        p.setY(majorFish.getY() + 50);
                    }
                    p.setPoint(-2);
                    points = Arrays.copyOf(points, points.length + 1); // 每有一个新对象就扩容数组
                    points[points.length - 1] = p;
                }
                if (npc instanceof Eliminate) {
                    for (SeaObject fish : fishes) {
                        if (fish instanceof FishScore) {
                            score += (fish.getScore()) / 2;
                        }
                    }
                    allNpc_SevenNum = 0;
                    fishes = new SeaObject[]{};

                    Point p = new Point();
                    p.setX(majorFish.getX());
                    if (majorFish.getY() > 150) {
                        p.setY(majorFish.getY() - 100);
                    } else {
                        p.setY(majorFish.getY() + 50);
                    }
                    p.setPoint(-1);
                    points = Arrays.copyOf(points, points.length + 1); // 每有一个新对象就扩容数组
                    points[points.length - 1] = p;
                }
                playTipMusic(EAT_TOOL);
            }
        }
    }

    // 行为(计时器)
    public void action() {

        java.util.Timer timer = new Timer(); //定时器对象
        int interval = 10; //定时间隔(以毫秒为单位)
        timer.schedule(new TimerTask() {
            public void run() { //定时干的事(每10毫秒自动执行)
                lX = majorFish.getX();       //存下10ms前的坐标
                lY = majorFish.getY();
                objectEnterAction(); // NPC鱼进入
                fishMoveAction(); // 鱼移动
                outOfBoundAction();//删除越界npc
                checkGameOverAction();
                eatFishesAction();
                continueMove();
                pointHiddenAction();
                repaint(); //重画(重新调用paint()方法)-----不要求掌握
            }
        }, interval, interval); //定时计划表
    }

    @Override
    public void paint(Graphics g) {
        // 将当前鼠标设置为透明状态
        Toolkit tk = Toolkit.getDefaultToolkit();
        Image img = tk.getImage("");
        Cursor cu = tk.createCustomCursor(img, new java.awt.Point(10, 10), "stick");
        this.setCursor(cu);

        switch (state) {
            case GAME_OVER:
//                g.setFont(new Font("Courier New", Font.BOLD, 60));
//                g.setColor(Color.WHITE);
//                g.drawString("GAME OVER", 550, 350);
                if (failFlag == false) {
                    failFlag = true;
                    Toolkit.getDefaultToolkit().beep();
                    JLabel lbel = new JLabel("你已死亡，游戏结束了！");
                    lbel.setFont(new Font("微软雅黑", Font.BOLD, 14));
                    int result = JOptionPane.showConfirmDialog(null, lbel, "游戏结束", JOptionPane.PLAIN_MESSAGE);
                    if (JOptionPane.YES_NO_OPTION == result) {
                        System.exit(0);
                    } else {
                        System.exit(0);
                    }
                }

            case RUNNING:
                if (checkpoint == SIMPLE_LEVEL) {
                    Images.background_simply.paintIcon(null, g, 0, 0);
                } else if (checkpoint == MEDIUM_LEVEL) {
                    Images.background_middle.paintIcon(null, g, 0, 0);
                } else {
                    Images.background_hard.paintIcon(null, g, 0, 0);
                }
                paintMajorFish(g);
                changeLevel();
                for (SeaObject fish : fishes) {
                    fish.paintImage(g);
                }
                for (Drop drop : drops) {
                    drop.paintImage(g);
                }
                for (Point p : points) {
                    p.paintImage(g);
                }
                // 显示进度分数、等级和生命
                g.setFont(new Font("Courier New", Font.BOLD, 24));
                g.setColor(Color.WHITE);
                g.drawString("SCORE:" + score, 900, 50);
                g.drawString("Level:" + majorFish.getLevel(), 1055, 50);
                g.drawString("LIFE:" + majorFish.getLife(), 1200, 50);
                if ((this.score >= 2000 && winFlag == false && checkpoint == SIMPLE_LEVEL) ||
                        (this.score >= 8000 && winFlag == false && checkpoint == MEDIUM_LEVEL)
                ) {
                    Toolkit.getDefaultToolkit().beep();
                    winFlag = true;
                    JLabel label = new JLabel("你已通过本关卡，可继续游玩！");
                    label.setFont(new Font("微软雅黑", Font.BOLD, 14));
                    JOptionPane.showMessageDialog(null, label, "胜利", JOptionPane.PLAIN_MESSAGE);
                }
        }
    }

    public void paintMajorFish(Graphics g) {//根据当前主角鱼情况绘制主角鱼
        if (flashFlag) {//当有大鱼吃到主角鱼时，开始延时每8毫秒显示主角鱼图片
            if (flashTime % 8 == 0) {
                majorFish.paintImage(g);
            }
            flashTime++;
            while (flashTime > 40) {//当主角鱼闪烁40次以后将主角鱼归为无闪烁情况
                flashFlag = false;
                flashTime = 0;
            }
        } else {//正常没有碰到主角时
            majorFish.paintImage(g);
        }
    }

}
